package com.shenma2009.controller;

import com.shenma2009.domain.ResultResponse;
import com.shenma2009.netty.client.WebSocketClient;
import com.shenma2009.service.ImitatePositionService;
import com.shenma2009.service.TVehicleFaultInfoService;
import com.shenma2009.vo.WebSocketInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;

/**
 * @author 军哥
 * @version 1.0
 * @description: 汽车终端模拟接口
 * @date 2023/5/27 9:39
 */

@RestController
@Slf4j
@RequestMapping(value = "/index")
public class IndexController {

    private static WebSocketClient webSocketClient = null;

    @Autowired
    private ImitatePositionService imitatePositionService;

    @Autowired
    private TVehicleFaultInfoService tVehicleFaultInfoService;

    /**
     * @description 连接websocket服务器
     * @author 军哥
     * @date 2023/5/27 10:04
     * @version 1.0
     */
    @PostMapping(value = "/open")
    public ResultResponse open(@RequestBody WebSocketInfoVo webSocketInfoVo) {
        if(webSocketClient != null) {
            if(webSocketClient.isAlive()) {
                return ResultResponse.SUCCESS();
            }
            else {
                webSocketClient.open();
            }
            return ResultResponse.SUCCESS();
        }
        else {
            webSocketClient = new WebSocketClient(webSocketInfoVo.getUrl());
            webSocketClient.open();
            return ResultResponse.SUCCESS();
        }
    }

    /**
     * @description 断开服务连接
     * @author 军哥
     * @date 2023/5/27 10:34
     * @version 1.0
     */

    @PostMapping(value = "/close")
    public ResultResponse close() {
        if(webSocketClient != null) {
            webSocketClient.close();
        }
        return ResultResponse.SUCCESS();
    }

    /**
     * @description 模拟基础故障
     * @author 军哥
     * @date 2023/5/27 10:33
     * @version 1.0
     */
    @PostMapping(value = "/sendBaseFault/{vin}")
    public ResultResponse sendBaseFault(@PathVariable("vin") String vin) {

        return imitatePositionService.sendBaseFault(webSocketClient, vin);
    }

    /**
     * @description 模拟车辆零配件故障组
     * @author 军哥
     * @date 2023/5/27 10:35
     * @version 1.0
     */
    @PostMapping(value = "/sendPartsFault/{vin}")
    public ResultResponse sendPartsFault(@PathVariable("vin") String vin) {
        return imitatePositionService.sendBaseFault(webSocketClient, vin);
    }

    /**
     * @description 模拟车辆电池故障组
     * @author 军哥
     * @date 2023/5/27 10:36
     * @version 1.0
     */
    @PostMapping(value = "/sendBatteryFault/{vin}")
    public ResultResponse sendBatteryFault(@PathVariable("vin") String vin) {
        return imitatePositionService.sendBaseFault(webSocketClient, vin);
    }

    /**
     * @description 模拟车辆历史历史轨迹
     * @author 军哥
     * @date 2023/5/27 10:38
     * @version 1.0
     */

    @PostMapping(value = "/sendHistoryFocus/{vin}")
    public ResultResponse sendHistoryFocus(@PathVariable("vin") String vin) {
        return imitatePositionService.sendBaseFault(webSocketClient, vin);
    }

    /**
     * @description 模拟发送当前车辆位置
     * @author 军哥
     * @date 2023/5/27 10:39
     * @version 1.0
     */
    @PostMapping(value = "/sendCurrentLocaltion/{vin}")
    public ResultResponse sendCurrentLocaltion(@PathVariable("vin") String vin) {
        return imitatePositionService.sendCurrentLocaltion(webSocketClient, vin);
    }
}
